import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


const routes = [
  {
    path:'/map',
    name: 'map',
    component:()=>import('@/components/MapContainer'),
  },
  {
    path: '/',
    redirect: '/index',
  },
  {
    path: '/login',
    name: 'login',
    component:()=>import('@/components/Login'),
  },
  {
    path: '/register',
    name: 'register',
    component:()=>import('@/components/Register'),
  },
  {
    path: '/resetPass',
    name: 'restPass',
    component:()=>import('@/components/ResetPass'),
  },
  {
    path: '/index',
    component: ()=>import('@/views/Index'),
    children: [{
        path: '',
        name: 'home',
        component:()=>import('../views/index/Home'),
      },
      {
        path:'/index/houseDetail/:id',
        name: 'houseDetail',
        component:()=>import('../views/index/HouseDetail'),
      },
      {
        path: 'news',
        name: 'news',
        component:()=>import('../views/index/News'),
      },
      {
        path: 'feedback',
        name: 'feedback',
        component:()=>import('../views/index/Feedback'),
      },
      {
        path:'agreement',
        name:'agreement',
        component:()=>import('../views/index/Agreement'),
      },
      {
        path:'pay',
        name: 'pay',
        component:()=>import('../views/index/Pay'),
      },
      {
        path:'rentType',
        name: 'rentType',
        component:()=>import('../views/index/RentType'),
      },
      {
        path: 'admin',
        component:()=>import('../views/Admin'),
        children:[{
          path:'',
          redirect:'info',
        },
          {
          path: 'info',
          name: 'info',
          component: () => import('../views/admin/AdminInfo')
        },
          {
          path:'house',
          name:'house',
          component:()=>import('../views/admin/AdminHouse')
        },
        {
          path:'houseCustom',
          name:'houseCustom',
          component:()=>import('../views/admin/HouseCustom')
        },
        {
          path:'myCustom',
          name:'myCustom',
          component:()=>import('../views/admin/MyCustom')
        },
        {
          path:'adminCustom',
          name:'adminCustom',
          component:()=>import('../views/admin/AdminCustom')
        },
        {
          path:'adminUser',
          name:'adminUser',
          component:()=>import('../views/admin/AdminUser')
        },
          {
          path:'order',
          name:'order',
          component:()=>import('../views/admin/AdminOrder')
        },
          {
          path:'adminNews',
          name:'adminNews',
          component:()=>import('../views/admin/AdminNews')
        },
          {
          path:'adminFeedback',
          name:'adminFeedback',
          component:()=>import('../views/admin/AdminFeedback')
        },
          {
          path:'collect',
          name:'collect',
          component:()=>import('../views/admin/AdminCollect')
        },
          {
          path:'adminHome',
          name:'adminHome',
          component:()=>import('../views/admin/AdminHome')
        },
          {
          path:'myFeedback',
          name:'myFeedback',
          component:()=>import('../views/admin/AdminMyFeedback')
        },
          {
          path:'release',
          name:'release',
          component:()=>import('../views/admin/AdminRelease')
        },
          {
          path:'password',
          name:'password',
          component:()=>import('../views/admin/AdminPassword')
        },
          {
          path:'adminAgreement',
          name:'adminAgreement',
          component:()=>import('../views/admin/AdminAgreement')
          },
          {
          path:'myOrder',
          name:'myOrder',
          component:()=>import('../views/admin/AdminMyOrder')
          },
          {
          path:'data',
          name:'data',
          component:()=>import('../views/admin/AdminData')
          },
          {
            path: 'termination',
            name: 'termination',
            component:()=>import('../views/admin/AdminTermination')
          },
          {
            path: 'myReservation',
            name: 'myReservation',
            component:()=>import('../views/admin/MyReservation')
          },
          {
            path: 'reservation',
            name: 'reservation',
            component:()=>import('../views/admin/AdminReservation')
          },
          ]
      }],
  },
  {
    path: '*',
    name: '404',
    component: ()=>import('../views/404')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  // 跳转后滚动到顶部
  scrollBehavior(to, from, savedPosition) {
    return { x: 0, y: 0 }
  }
})

//路由守卫
router.beforeEach((to, from, next) => {

  if (to.path === '/login'){
    next()
  }

  const isLogin = localStorage.getItem('login-user') ? true : false
  if (to.path == '/index/admin/info' || to.path == '/index/admin/adminHome' ||to.path == '/index/admin/house' || to.path == '/index/admin/order' || to.path == '/index/admin/adminUser' || to.path == '/index/admin/adminFeedback' || to.path == '/index/admin/adminNews' || to.path == '/index/admin/home' || to.path =='/index/admin/collect' || to.path == '/index/admin/release' || to.path == '/index/admin/myFeedback' || to.path == '/index/admin/password' || to.path == '/index/admin/myOrder' || to.path == '/index/admin/password'){
    if (isLogin){
      next()
    }else {
      next('/index')
    }
  }else {
    next()
  }
})

export default router
